package com.ddone.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author ddone
 * @date 2024/3/17-23:59
 */
public class MybatisUtil {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        //此处已经固定命名了！mybatis.xml 注意：配置文件的命名已经固定了！
        try (InputStream stream = Resources.getResourceAsStream("mybatis-config.xml")) {
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 返回不自动提交对sqlSession对象 DQL
     * @return
     */
    public static SqlSession openSession(){

        return sqlSessionFactory.openSession();
    }

    /**
     * 返回自动提交的sqlSession对象 DML
     * @return
     */
    public static SqlSession openAutoSession(){

        return sqlSessionFactory.openSession(true);
    }


}
